﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ActiveStory.DataManager
{
    public class ActiveStoryPage
    {
        #region Constructors
        /// <summary>
        /// Default Constructor. Sets pageName to String.Empty, pageNumber to -1, buttonCounter to 0, buttonToPageMap
        /// to a new, empty Dictionary.
        /// Disabled.
        /// </summary>
        private ActiveStoryPage()
        {
            _pageName = String.Empty;
            _pageNumber = -1;
            _buttonCounter = 0;
            _buttonToPageMap = new Dictionary<int, int>();
            _xamlFileLocation = String.Empty;
            _thumbnailFileLocation = String.Empty;
        }
        /// <summary>
        /// New page constructor. Used for a new page without any previous data. pageName and pageNumber are set from the 
        /// parameters. buttonCounter is set to 0 and buttonToPageMap is set to a new, empty Dictionary.
        /// </summary>
        /// <param name="pageName">The name for the page. Can be String.Empty.</param>
        /// <param name="pageNumber">The page number. Must be a positive number.</param>
        public ActiveStoryPage(String pageName, int pageNumber, String xamlFileLocation, String thumbnailFileLocation)
        {
            _pageName = pageName;
            if (pageNumber < 0)
                _pageNumber = 0;
            else
                _pageNumber = pageNumber;
            _buttonCounter = 0;
            _buttonToPageMap = new Dictionary<int, int>();
            _xamlFileLocation = xamlFileLocation;
            _thumbnailFileLocation = thumbnailFileLocation;
        }
        /// <summary>
        /// Loaded page constructor. Used for a new page without any previous data. pageName, pageNumber, buttonCounter and
        /// buttonToPageMap are set from the parameters.
        /// </summary>
        /// <param name="pageName">The name for the page. Can be String.Empty.</param>
        /// <param name="pageNumber">The page number. Must be a positive number.</param>
        /// <param name="buttonCounter">The counter value loaded from the file.</param>
        /// <param name="buttonToPageMap">The button to page mappings, loaded from the file.</param>
        public ActiveStoryPage(String pageName, int pageNumber, int buttonCounter, String xamlFileLocation, String thumbnailFileLocation, Dictionary<int, int> buttonToPageMap)
        {
            _pageName = pageName;
            if (pageNumber < 0)
                _pageNumber = 0;
            else
                _pageNumber = pageNumber;
            if (buttonCounter < 0)
                _buttonCounter = 0;
            else
                _buttonCounter = buttonCounter;
            if (buttonToPageMap==null)
                _buttonToPageMap=new Dictionary<int,int>();
            else
                _buttonToPageMap = buttonToPageMap;
            _xamlFileLocation = xamlFileLocation;
            _thumbnailFileLocation = thumbnailFileLocation;
        }
        #endregion
        #region Fields
        /// <summary>
        /// Name of the page. Can be String.Empty
        /// </summary>
        protected string _pageName = String.Empty;
        /// <summary>
        /// The page number. Must be positive.
        /// </summary>
        protected int _pageNumber = -1;
        /// <summary>
        /// The button counter, used to number buttons as they are added.
        /// This must be done manually when adding buttons and IncrementCount() must also be
        /// called whenever a number is used.
        /// </summary>
        protected int _buttonCounter = 0;
        /// <summary>
        /// Mapping of buttons to pages. Used for linking buttons to pages.
        /// Each button can only point to one page. A page can be linked to by multiple buttons.
        /// </summary>
        protected Dictionary<int,int> _buttonToPageMap;
        /// <summary>
        /// Location of xaml file (if the page is new, this indicates where to save the xaml file,
        /// if its loaded it indicates where the existing xaml file is for it to be overwritten)
        /// </summary>
        protected string _xamlFileLocation = String.Empty;
        /// <summary>
        /// Location of thumbnail file (if the page is new, this indicates where to save the thumbnail file,
        /// if its loaded it indicates where the existing thumbnail file is for it to be overwritten)
        /// </summary>
        protected string _thumbnailFileLocation = String.Empty;
        #endregion
        #region Properties
        /// <summary>
        /// Name of the page. Can be String.Empty
        /// </summary>
        public String PageName
        {
            get
            {
                return _pageName;
            }
            set
            {
                _pageName = value;
            }
        }
        /// <summary>
        /// The page number. Must be positive.
        /// </summary>
        public int PageNumber
        {
            get
            {
                return _pageNumber;
            }
        }
        /// <summary>
        /// The button counter, used to number buttons as they are added.
        /// This must be done manually when adding buttons and IncrementCount() must also be
        /// called whenever a number is used.
        /// </summary>
        public int ButtonCounter
        {
            get
            {
                return _buttonCounter;
            }
        }
        /// <summary>
        /// Mapping of buttons to pages. Used for linking buttons to pages.
        /// Each button can only point to one page. A page can be linked to by multiple buttons.
        /// </summary>
        public Dictionary<int, int> ButtonToPageMap
        {
            get
            {
                return _buttonToPageMap;
            }
        }
        public String XamlFileLocation
        {
            get
            {
                return _xamlFileLocation;
            }
        }
        public String ThumbnailFileLocation
        {
            get
            {
                return _thumbnailFileLocation;
            }
        }
        #endregion
        #region Methods
        /// <summary>
        /// Must be called whenever a number is used when adding buttons.
        /// </summary>
        public void IncrementButtonCount()
        {
            _buttonCounter++;
        }
        /// <summary>
        /// Adds a mapping (for links) of a button to a page.
        /// </summary>
        /// <param name="buttonNumber">The button which causes the transition.</param>
        /// <param name="pageNum">The page which  must be transitioned to.</param>
        public void AddButtonMapping(int buttonNumber, int pageNum)
        {
            if (buttonNumber < 0)
                throw new Exception("ActiveStoryPage.RemoveButtonMapping():Invalid button number");
            if (pageNum < 0)
                throw new Exception("ActiveStoryPage.RemoveButtonMapping():Invalid page number");
            if (ButtonToPageMap.ContainsKey(buttonNumber))
                ButtonToPageMap.Remove(buttonNumber);
            ButtonToPageMap.Add(buttonNumber, pageNum);
        }
        /// <summary>
        /// Removes a mapping (for links) of a button to a page.
        /// </summary>
        /// <param name="buttonNumber">The button which causes the transition.</param>
        public void RemoveButtonMapping(int buttonNumber)
        {
            if (buttonNumber < 0)
                throw new Exception("ActiveStoryPage.RemoveButtonMapping():Invalid button number");
            if (ButtonToPageMap.ContainsKey(buttonNumber))
                ButtonToPageMap.Remove(buttonNumber);
        }
        #endregion
    }
}
